package com.spukmk3me.animatorpack.pack0;

import com.spukmk3me.scene.Animator;
import com.spukmk3me.scene.Node;
import com.spukmk3me.util.ContinuousTimedAnimation;
import com.spukmk3me.math.FPMath;
import com.spukmk3me.math.Matrix4f;

public class TransformationAnimator extends Animator
{
    public TransformationAnimator()
    {
        m_animation = new ContinuousTimedAnimation();
    }
    
    public void setTransformations( Matrix4f[] trans )
    {
        m_trans = trans;
    }
    
    public Matrix4f[] getTransformations()
    {
        return m_trans;
    }
    
    @Override
    protected void onNodePrepareToChange( Node newNode ) {}
    
    @Override
    protected void onNodeChanged() {}

    @Override
    protected void animate( int deltaTime )
    {
        if ( !isAnimating() )
            return;

        m_animation.execute( deltaTime );
        
        if ( m_smooth )
        {
            /*if ( m_animation.isLastAnimatedFrame() )
                getNode().getMatrix().copy( m_trans[ m_animation.getFrameIndex() ] );
            else
            {
                int realIndex   = m_animation.getFrameIndex_real();
                int index1, index2;
                float factor;
                
                if ( m_animation.getDirection() )
                {
                    index1 = (realIndex & 0xFFFF0000) >> 16;
                    index2 = m_animation.getNextFrameIndex( index1, 1 );
                    factor = (realIndex - index1) / 0x00010000f;
                }
                else
                {
                    index1 = ((realIndex + 0x00010000) & 0xFFFF0000) >> 16;
                    index2 = m_animation.getNextFrameIndex( index1, 1 );
                    factor = (index1 - realIndex) / 0x00010000f;
                }
                
                m_smoothBuffer1.copy( m_trans[ index1 ] );
                m_smoothBuffer1.mul( factor );
                m_smoothBuffer2.copy( m_trans[ index2 ] );
                m_smoothBuffer2.mul( 1 - factor );
                m_smoothBuffer1.add( m_smoothBuffer2 );
                getNode().getMatrix().copy( m_smoothBuffer1 );
            }*/
        }
        else
        {
            int index = m_animation.getCurrentIndex();
            
            if ( index == ContinuousTimedAnimation.LASTINDEX_TOKEN )
                getNode().c( m_trans[ m_trans.length + 1 ] );
            else
                getNode().c( m_trans[ m_animation.getLeftIndex() ] );
        }
    }
    
    /**
     *  Setup the animation.
     *  @param firstIndex The first frame index of animated frame sequence.
     *  @param lastFrame The last frame index of animated frame sequence.
     *  @param msPerFrame Amount of time for a frame, measured in milliseconds.
     * Pass -1 value to this parameter if you want to keep the old msPerFrame.
     *  @param nFrameToStop The animation will stop after nFrameToStop frames.
     * Ignored if the mode doesn't contain MODE_FRAMESTOP.
     */
    public final void setAnimating( int mode, int firstIndex, int lastIndex, int[] times,
        int time, int limitParam, boolean useSmooth )
    {
        m_animation.setAnimation( mode, firstIndex, lastIndex, times, time, limitParam );
        m_smooth = useSmooth;
                
        if ( useSmooth )
        {
            if ( m_smoothBuffer1 == null )
            {
                m_smoothBuffer1 = new Matrix4f();
                m_smoothBuffer2 = new Matrix4f();
            }
        }
    }

    /**
     *  Check if the sprite is animating or not.
     *  @return true if this sprite is animating, otherwise return false.
     */
    public final boolean isAnimating()
    {
        return m_animation.isAnimating();
    }
    
    private Matrix4f[]                  m_trans;
    private Matrix4f                    m_smoothBuffer1, m_smoothBuffer2;
    private ContinuousTimedAnimation    m_animation;
    private boolean                     m_smooth;
}